package app.framework.component;

import app.framework.component.sequence.Sequence;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 全局唯一ID生成组件
 * Created by shadowedge on 2018/11/23.
 */
@Component
public class Sequencer {
    @Value("${workerId:#{1}}")
    private int workerId;
    @Value("${dataCenterId:#{1}}")
    private int dataCenterId;
    private Sequence idWorker;

    @Bean
    private String  initSequencer(){
        idWorker = new Sequence(workerId, dataCenterId);
        return "success";
    }

    public Long getId(){
        return idWorker.nextId();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            Long id = new Sequence(1, 1).nextId();
            System.out.println(id);
//            System.out.println(id.hashCode());
        }
    }

}
